package com.wh.simpleredis.serializer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.Charset;

/**
 * @Auther: HuiEr
 * @Date: 2020/3/10 20:14
 * @Description
 */
public class CustomSerializer implements RedisSerializer {
    private Class clazz;
    public CustomSerializer(Class clazz){
        this.clazz=clazz;
    }
    @Override
    public byte[] serialize(Object o) throws SerializationException {
        String string = JSON.toJSONString(o);
        return string.getBytes(Charset.forName("UTF-8"));
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        String s = new String(bytes);
        return JSON.parseObject(s,clazz);
    }
}
